<?php defined('SYSPATH') OR die('No direct access allowed.');

class Form_Login_Model extends Model{
	private $o_Post;
	private $a_FormContent;
	//private $a_VerifySection;
	//protected $db;
	
	public function __construct(){
		parent::__construct();
		new Session;
		$this->auth = Auth :: instance();
	}
	
	private function parse_data(){
		$this->o_Post = new Validation($_POST);
		
		$this->o_Post	-> pre_filter( 'trim' )
      					-> pre_filter( 'htmlspecialchars' );
		$this->o_Post->add_rules('username', 'required');
		$this->o_Post->add_rules('password', 'required');
		$this->o_Post->add_rules('log', 'required');
		
		return $this->o_Post->validate();
	}
	
/*	private function check_data() {
		$this->db = new Database();
		$this->a_FormContent = $_POST;
		$records = $this->db->select('username', 'password')->from('users')->where(array('username'=> $this->a_FormContent['login'], 'password'=>sha1($this->a_FormContent['pass'])))->get();
		
		if ($records->count()){
			return TRUE;
		}
		else {
			return FALSE;
		}
	}*/
	
	public function receive_data(){
		
		if ($this->parse_data()){
			$user = ORM :: factory('user', $this->o_Post->username);
			
			if ($this->auth->login ($user, $this->o_Post->password)){
				$this->a_FormContent['info'] = Kohana :: lang ('login.successful');
			}
			else{
				$this ->a_FormContent['info'] = Kohana :: lang ('login.error');
			}
		}
		else {
			$this->a_FormContent['errors'] = $this->o_Post->errors();
		}
		return $this->a_FormContent;
	}
	

}

?>